<template>
    <s-layout id="layout" :title="'发布公告'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="'#ffffff'" :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
        <view class="call_on list_box" :style="{paddingTop:(safeAreaInsets?.top + 44) + 'px'}">
            <!-- <scroll-view  scroll-y class="comprehensive" >
                <view class="item_wrap">
                    <view class="detail_title">{{ lists.title }}</view>
                    <view class="detail_time">{{ formatDatetime(lists.createtime) }}</view>
                    <view class="content">
                        <u-parse :content="lists.content" ></u-parse>
                    </view>
                </view>
            </scroll-view> -->
            
            <view class="announce_box">
                <view class="input_box">
                    <uni-easyinput type="number" placeholder="请输入标题" v-model="title" :inputBorder="true">
                    </uni-easyinput>
                </view>
                <editor id="editor" class="ql-container" placeholder="请输入内容" style="background-color: #F9F9F9; padding: 20rpx;font-style: normal;" @input="contentInput(index, $event)" @statuschange="onStatusChange">
                </editor>
                <view class="upload_box">
                    <u-upload :fileList="fileList" @afterRead="imageUpload" @delete="deletePic" :name="index" :multiple="true" :maxCount="1">
                    </u-upload>
                </view>
            </view>
        </view>
    </s-layout>

    <!-- 底部保存按钮 -->
    <view>
        <s-button @netWork="submitForm" :text="'提交'" />
    </view>
</template>
<script setup>
    import { formatDatetime } from '@/utils/fn.js';
    import sheep from '@/sheep';
    import { onLoad } from "@dcloudio/uni-app";
    import { ref,reactive } from 'vue';
    const { safeAreaInsets } = uni.getSystemInfoSync()
    const userStoreValue = uni.getStorageSync('user-store');
    const userStoreObject = JSON.parse(userStoreValue)
    onLoad((options) => {
        
    })

    // 内容
    const contentInput = (index,e) => {
        content.value = e.detail.html
        console.log(e,'input_e');
    }

    // 提交的数据
    const title = ref()             // 标题
    const content = ref()           // 内容
    const company_type = ref()      // 公司类型 1=劳务派遣 2=外包
    const image = ref()             // 图片
    const fileList = ref([])

    // 删除图片
    const deletePic = (event) => {
        image.value = ''
        fileList.value = []
    };

    // 新增图片
    const imageUpload = async (event) => {
        let lists = [].concat(event.file);
        let { url } = await sheep.$api.app.upload(lists[0].url, 'ugc')
        fileList.value.push({
            url: sheep.$url.cdn(url),
            status: 'success',
            message: ''
        });
        image.value = url
    };
    // 提交
    const submitForm = () => {
        if(userStoreObject.userInfo.group_id == 10) {
            company_type.value = 1
        } else if (userStoreObject.userInfo.group_id == 13) {
            company_type.value = 2
        }

        if(title.value) {
            if(content.value || image.value) {
                let params = {
                    title: title.value,
                    content: content.value,
                    department_id:userStoreObject.userInfo.department.deparment.id,
                    company_type:company_type.value,
                    image:image.value,
                }
                uni.$u.throttle(async () => {
                    const resust = await sheep.$api.notification.notificationProjectAdd(params);
                    console.log(resust),'走接口';
                    if (resust.code == 1) {
                        sheep.$helper.toast(resust.msg);
                        setTimeout(() => {
                            sheep.$router.back(true)
                        }, 500)
                    } else {
                        sheep.$helper.toast(resust.msg);
                    }
                },1000)
            } else {
                sheep.$helper.toast('请填写发布内容！');
            }
        } else {
            sheep.$helper.toast('请输入发布标题！');
        }
    }

</script>
<style lang="scss" scoped>
    #editor::placeholder {
        font-style: normal;
    }

    //列表
    .call_on{
        background-color: #eeeeee;
        width: 100%;
        .list_box{
            width: 100%;
            height: auto;
        }
    }

    .announce_box {
        background-color: #ffffff;
        width: calc(100% - 80rpx);
        margin: 20rpx 20rpx;
        height: calc(80vh - 80rpx);
        padding: 20rpx 20rpx;
        border-radius: 30rpx;
        
    }

    .upload_box {
        margin-top: 20rpx;
    }

    .input_box {
        margin-bottom: 20rpx;
    }
</style>